package com.aliyun.solutions.demo.wanxiang.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;

    // 从系统环境中读取环境变量，默认为true（如果环境变量未设置）
    @Value("${SECURITY_ENABLED:false}")
    private boolean securityEnabled;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (securityEnabled) {
            // 如果开启了安全认证，则添加拦截器
            registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/api/**");
        }
        // 如果未开启，则不添加拦截器
    }
}